import requests
import logging


class RequestClient:
    def __init__(self,url,timeout):
        self.url = url
        self.timeout = timeout
        self.session = requests.session()

    def send_request(self,method,url,params=None,data=None,json=None,headers=None):
        url = f"{self.url}{url}"
        logging.info(f"发起{method}请求")
        logging.info(f"请求参数: params={params}, data={data}, json={json}")
        count = 0
        while count < 3:
            try:
                response = self.session.request(
                    method=method.upper(),
                    url=url,
                    params=params,
                    data=data,
                    json=json,
                    headers=headers
                )
                logging.info(f"响应状态码:{response.status_code}")
                return response
            except Exception as e:
                print(e)
                count += 1
        logging.error("请求重试次数耗尽，请求失败")
        raise Exception("请求重试次数耗尽")

    def get(self, url, **kwargs):
        return self.send_request(method="GET", url=url, **kwargs)

    def post(self, url, **kwargs):
        return self.send_request(method="POST", url=url, **kwargs)

    def put(self, url, **kwargs):
        return self.send_request(method="PUT", url=url, **kwargs)

    def delete(self, url, **kwargs):
        return self.send_request(method="DELETE", url=url, **kwargs)
